class Solution {
    List<List<Integer>> re = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        //没有比target小的数，返回空
        Arrays.sort(candidates);
        //回溯
        add(candidates,0,target);
        return re;
    }
    public void add(int[] candidates,int begin,int target){
        if(target ==0 ){
            re.add(new ArrayList<Integer>(temp));
            return;
        }
        int len = candidates.length;
        for(int i = begin ; i < len ; i++){
            //剪枝
            if(candidates[i] > target) break;
            class Solution {
    List<List<Integer>> re = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        //没有比target小的数，返回空
        Arrays.sort(candidates);
        //回溯
        add(candidates,0,target);
        return re;
    }
    public void add(int[] candidates,int begin,int target){
        if(target ==0 ){
            re.add(new ArrayList<Integer>(temp));
            return;
        }
        int len = candidates.length;
        for(int i = begin ; i < len ; i++){
            //剪枝
            if(candidates[i] > target) break;
            //每次都从begin开始，这样可以避免出现重复的数组
            temp.add(candidates[i]);
            add(candidates,i,target - candidates[i]);
            temp.remove(temp.size()-1);
        }
    }
}
            temp.add(candidates[i]);
            add(candidates,i,target - candidates[i]);
            temp.remove(temp.size()-1);
        }
    }
}